package com.scarllet.springbootaop.aspect;

import com.scarllet.springbootaop.annotation.SysLog;
import com.scarllet.springbootaop.entity.SysLogEntity;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author gxcheng
 * @date 2019/5/15
 * 操作日志使用
 */
@Slf4j
@AllArgsConstructor
@Aspect
@Component
public class SysLogAspect {

    /**
     * @author gxcheng
     * @date 2019/5/15
     * 在这里，我们可以拿到方法名称上面的注解信息
     */
    @SneakyThrows
    @Around("@annotation(sysLog)")
    public Object aroud(ProceedingJoinPoint point, SysLog sysLog){
        Object obj = point.proceed();
        String strMethodName = point.getSignature().getName();
        SysLogEntity logEntity=new SysLogEntity();
        logEntity.setTitle(sysLog.value());
        logEntity.setType(sysLog.operationType().getValue());
        logEntity.setMethod(strMethodName);
        log.info(logEntity.toString());
        return obj;
    }
}
